package com.zdsoft.blp.zongbuju.shouye.taoshihui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.gongyong.dialog.ShowOrDismissDialog;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.imageFrame.IvFrameView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.Goods;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/9/1.
 */
public class ShangpxqActivity extends BaseActivity implements View.OnClickListener,NetCallback{
    private FanHuiView fh;
    private TextView tv_yPrice,tv_xPrice,tv_peopleBuy,tv_total,tv_renshu,tv_merchantName,tv_merchantAddr,
            tv_goodsName,tv_summary,tv_infoTitle,tv_infoContent,tv_xzTitle,tv_xzContent;
    private Button bt_lijigm;
    private RelativeLayout rl_other;
    private IvFrameView iv_datu;
    private String goodsNum = null;
    private String goodsId = null;
    @Override
    protected void init() {
        setContentView(R.layout.tsh_shangpxq);

        fh = find(R.id.fh);
        bt_lijigm = find(R.id.bt_lijigm);
        tv_yPrice = find(R.id.tv_yPrice);
        rl_other = find(R.id.rl_other);
        iv_datu = find(R.id.iv_datu);
        tv_xPrice = find(R.id.tv_xPrice);
        tv_peopleBuy = find(R.id.tv_peopleBuy);
        tv_total = find(R.id.tv_total);
        tv_renshu = find(R.id.tv_renshu);
        tv_merchantName = find(R.id.tv_merchantName);
        tv_merchantAddr = find(R.id.tv_merchantAddr);
        tv_goodsName = find(R.id.tv_goodsName);
        tv_summary = find(R.id.tv_summary);
        tv_infoTitle = find(R.id.tv_infoTitle);
        tv_infoContent = find(R.id.tv_infoContent);
        tv_xzTitle = find(R.id.tv_xzTitle);
        tv_xzContent = find(R.id.tv_xzContent);

        fh.getTv_title().setText("商品详情");
        
        goodsId =getIntent().getStringExtra("goodsId");
        ShowOrDismissDialog.show(ShangpxqActivity.this);
        getGoodsInfo(goodsId);

        bt_lijigm.setOnClickListener(this);
        rl_other.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.bt_lijigm:
                intent = new Intent(getApplicationContext(),OrderActivity.class);
                intent.putExtra("goodsName",tv_goodsName.getText().toString());
                intent.putExtra("discountPrice",tv_xPrice.getText().toString());
                intent.putExtra("goodsNum",goodsNum);
                intent.putExtra("goodsId",goodsId);
                break;
            case R.id.rl_other:
                intent = new Intent(getApplicationContext(),FenLeiActivity.class);
                intent.putExtra("merchantName",tv_merchantName.getText().toString());
                intent.putExtra("type",2);
                break;
        }
        startActivity(intent);
    }

    public void getGoodsInfo(String id ) {
        Net net = new Net();
        net.url = API.GOODS_URL;
        net.nameSpace = API.GOODS_NAMESPACE;
        net.methodName = API.GOODSCONTENT;
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        net.parms = map;
        getServer(net,this);
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag){
            try {
                JSONObject object = new JSONObject(res);
                tv_yPrice.setText(object.getString("oldPrice")+"元");
                tv_yPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//设置原价
                tv_xPrice.setText(object.getString("discountPrice")); //设置现价
                tv_peopleBuy.setText(object.getString("peopleBuy"));//设置已售
                tv_total.setText(object.getString("total")+"分");//评分
                tv_renshu.setText(object.getString("evaluationCount"));//评价人数
                tv_merchantName.setText(object.getString("merchantName"));//店名
                tv_merchantAddr.setText(object.getString("merchantAddr"));//店家地址
                goodsNum=object.getString("goodsNum");//店家地址
                List<String> list = new ArrayList<>();
                JSONArray array = object.getJSONArray("picPathSet");
                for(int i =0;i<array.length();i++){
                    JSONObject imObject = array.getJSONObject(i);
                    list.add(imObject.getString("picPath"));
                }
                iv_datu.show(list);//大图轮播
                tv_goodsName.setText(object.getString("goodsName"));//标题
                tv_summary.setText(object.getString("summary"));//简介
                JSONArray contentArray = object.getJSONArray("details");
                for(int i = 0 ;i<contentArray.length();i++){
                    JSONObject infoContent = contentArray.getJSONObject(i);
                   if(i==0){
                       tv_infoTitle.setText(infoContent.getString("title"));
                       tv_infoContent.setText(Html.fromHtml(infoContent.getString("content")));
                   }else if(i==1){
                       tv_xzTitle.setText(infoContent.getString("title"));
                       tv_xzContent.setText(Html.fromHtml(infoContent.getString("content")));
                   }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        ShowOrDismissDialog.dismiss();
    }
}
